package com.peppa.petshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.peppa.petshop.entity.*;
import com.peppa.petshop.mapper.*;
import com.peppa.petshop.service.UserService;
import com.peppa.petshop.vo.DataVO;
import com.peppa.petshop.vo.IndentItemVO;
import com.peppa.petshop.vo.IndentVO;
import com.peppa.petshop.vo.PetVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author SongTiantian
 * @create 2021-06-09-9:13
 */
@Service
public class UserServiceImpl extends ServiceImpl<IndentMapper, Indent> implements UserService  {
    @Autowired(required = false)
    private IndentMapper indentMapper;
    @Autowired(required = false)
    private CustomerMapper customerMapper;
    @Autowired(required = false)
    private AddressMapper addressMapper;
    @Autowired(required = false)
    private IndentItemMapper indentItemMapper;
    @Autowired(required = false)
    private GoodMapper goodMapper;
    @Autowired(required = false)
    private CategoryMapper categoryMapper;

    @Override
    public List<PetVO> getPetList() {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.gt("count", 0);
        List<Good> pets = goodMapper.selectList(queryWrapper);
        List<PetVO> petVOS = new ArrayList<>();
        for (Good good:pets){
            PetVO petVO = new PetVO();
            BeanUtils.copyProperties(good,petVO);
            String imagePaths = petVO.getImagePath();
            String[] split = imagePaths.split(";");
            String s =split[0] ;
            System.out.println(s);
//            System.out.println(imagePaths);
            petVO.setImagePath(s);
            petVO.setHref("/user/pet?goodId="+good.getGoodId());
            petVOS.add(petVO);
        }
        return petVOS;
    }

    @Override
    public PetVO getPet(Integer goodId) {
        PetVO petVO = new PetVO();
        Good good = goodMapper.selectById(goodId);
        BeanUtils.copyProperties(good,petVO);
        String imagePaths = petVO.getImagePath();
        String[] split = imagePaths.split(";");
        String s ="";
        for (int i = 0; i < split.length; i++) {
            s = s + "http://localhost:8080" + split[i]+";";
        }
        System.out.println(s);
        petVO.setImagePath(s);
        petVO.setHref("http://localhost:8080/user/pet?goodId="+good.getGoodId());
        return petVO;
    }

    @Override
    public Customer register( String phone, String password) {
        Customer customer = new Customer(null, phone, phone, password, false, LocalDateTime.now(), 0, LocalDateTime.now());
        int insert = customerMapper.insert(customer);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("phone",phone);
        queryWrapper.eq("password",password);
        Customer one = customerMapper.selectOne(queryWrapper);
        return one;
    }

    @Override
    public List<PetVO> getNewPetList() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("is_hot",false);
        queryWrapper.eq("is_missing",false);
        queryWrapper.gt("count", 0);
        List<Good> pets = goodMapper.selectList(queryWrapper);
        List<PetVO> petVOS = new ArrayList<>();
        for (Good good:pets){
            PetVO petVO = new PetVO();
            BeanUtils.copyProperties(good,petVO);
            Category category = categoryMapper.selectById(good.getCategory());
            petVO.setCategory(category.getCategory());
            String imagePaths = petVO.getImagePath();
            String[] split = imagePaths.split(";");
            String s =split[0] ;
            System.out.println(s);
            petVO.setImagePath(s);
            petVO.setHref("/user/pet?goodId="+good.getGoodId());
            petVOS.add(petVO);
        }
        return petVOS;
    }

    @Override
    public List<PetVO> getHotPetList() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("is_hot",true);
        queryWrapper.gt("count", 0);
        List<Good> pets = goodMapper.selectList(queryWrapper);
        List<PetVO> petVOS = new ArrayList<>();
        for (Good good:pets){
            PetVO petVO = new PetVO();
            BeanUtils.copyProperties(good,petVO);
            Category category = categoryMapper.selectById(good.getCategory());
            petVO.setCategory(category.getCategory());
            String imagePaths = petVO.getImagePath();
            String[] split = imagePaths.split(";");
            String s =split[0] ;
            System.out.println(s);
            petVO.setImagePath(s);
            petVO.setHref("/user/pet?goodId="+good.getGoodId());
            petVOS.add(petVO);
        }
        return petVOS;
    }

    @Override
    public List<PetVO> searchPets(String name) {
        System.out.println(name);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("good_name",name);
        queryWrapper.gt("count", 0);
        List<Good> pets = goodMapper.selectList(queryWrapper);
        List<PetVO> petVOS = new ArrayList<>();
        for (Good good:pets){
            PetVO petVO = new PetVO();
            BeanUtils.copyProperties(good,petVO);
            Category category = categoryMapper.selectById(good.getCategory());
            petVO.setCategory(category.getCategory());
            String imagePaths = petVO.getImagePath();
            String[] split = imagePaths.split(";");
            String s =split[0] ;
            System.out.println(s);
            petVO.setImagePath(s);
            petVO.setHref("/user/pet?goodId="+good.getGoodId());
            petVOS.add(petVO);
        }
        return petVOS;
    }

    @Override
    public List<PetVO> searchCategory(Integer categoryId) {
        System.out.println(categoryId);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("category",categoryId);
        queryWrapper.gt("count", 0);
        List<Good> pets = goodMapper.selectList(queryWrapper);
        List<PetVO> petVOS = new ArrayList<>();
        for (Good good:pets){
            PetVO petVO = new PetVO();
            BeanUtils.copyProperties(good,petVO);
            Category category = categoryMapper.selectById(good.getCategory());
            petVO.setCategory(category.getCategory());
            String imagePaths = petVO.getImagePath();
            String[] split = imagePaths.split(";");
            String s =split[0];
            System.out.println(s);
            petVO.setImagePath(s);
            petVO.setHref("/user/pet?goodId="+good.getGoodId());
            petVOS.add(petVO);
        }
        return petVOS;
    }

    @Override
    public List<PetVO> getLovePetList() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("is_missing",true);
        queryWrapper.gt("count", 0);
        List<Good> pets = goodMapper.selectList(queryWrapper);
        List<PetVO> petVOS = new ArrayList<>();
        for (Good good:pets){
            PetVO petVO = new PetVO();
            BeanUtils.copyProperties(good,petVO);
            Category category = categoryMapper.selectById(good.getCategory());
            petVO.setCategory(category.getCategory());
            String imagePaths = petVO.getImagePath();
            String[] split = imagePaths.split(";");
            String s =split[0] ;
            System.out.println(s);
            petVO.setImagePath(s);
            petVO.setHref("/user/pet?goodId="+good.getGoodId());
            petVOS.add(petVO);
        }
        return petVOS;
    }

    @Override
    public List<PetVO> getPetListOrderBy(String orderBy) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (orderBy.equals("high")){
            queryWrapper.orderByDesc("price");
        }else{
            queryWrapper.orderByAsc("price");
        }
        queryWrapper.gt("count", 0);
        List<Good> pets = goodMapper.selectList(queryWrapper);
        List<PetVO> petVOS = new ArrayList<>();
        for (Good good:pets){
            PetVO petVO = new PetVO();
            BeanUtils.copyProperties(good,petVO);
            Category category = categoryMapper.selectById(good.getCategory());
            petVO.setCategory(category.getCategory());
            String imagePaths = petVO.getImagePath();
            String[] split = imagePaths.split(";");
            String s =split[0] ;
            System.out.println(s);
            petVO.setImagePath(s);
            petVO.setHref("/user/pet?goodId="+good.getGoodId());
            petVOS.add(petVO);
        }
        return petVOS;
    }

    @Override
    public List<IndentVO> getIndentList(Integer customerId) {

        List<IndentVO> indentVOs = new ArrayList<>();
        QueryWrapper queryWrapper1 = new QueryWrapper();
//        queryWrapper1.eq("indent_status",indentStatus);
        queryWrapper1.eq("customer",customerId);
        List<Indent> indents = indentMapper.selectList(queryWrapper1);

        for (Indent indent : indents) {
            IndentVO indentVO = new IndentVO();
            BeanUtils.copyProperties(indent,indentVO);
            indentVO.setCustomer(customerMapper.selectById(indent.getCustomer()).getCustomerName());
            indentVO.setAddress(addressMapper.selectById(indent.getAddress()).getAddressName());
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("indent",indent.getIndentId());
            List<IndentItem> items = indentItemMapper.selectList(queryWrapper);
            ArrayList<IndentItemVO> itemVOs = new ArrayList<>();
            for (IndentItem item : items) {
                IndentItemVO itemVO = new IndentItemVO();
                BeanUtils.copyProperties(item,itemVO);
                Good good = goodMapper.selectById(item.getGood());
                BeanUtils.copyProperties(good,itemVO);
                String imagePath = good.getImagePath();
                itemVO.setImagePath( imagePath.split(";")[0]);
                itemVO.setGood(good.getGoodName());
                itemVO.setGoodId(good.getGoodId());
                itemVO.setUnivalence(good.getPrice());
                itemVOs.add(itemVO);
            }
            indentVO.setIndentItems(itemVOs);
            indentVOs.add(indentVO);
        }
        return indentVOs;
    }

    @Override
    public List<IndentVO> getDeliveryIndents(Integer customerId) {
        List<IndentVO> indentVOs = new ArrayList<>();
        QueryWrapper queryWrapper1 = new QueryWrapper();
//        queryWrapper1.eq("indent_status",indentStatus);
        queryWrapper1.eq("customer",customerId);
        queryWrapper1.eq("indent_status",1);
        List<Indent> indents = indentMapper.selectList(queryWrapper1);

        for (Indent indent : indents) {
            IndentVO indentVO = new IndentVO();
            BeanUtils.copyProperties(indent,indentVO);
            indentVO.setCustomer(customerMapper.selectById(indent.getCustomer()).getCustomerName());
            indentVO.setAddress(addressMapper.selectById(indent.getAddress()).getAddressName());
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("indent",indent.getIndentId());
            List<IndentItem> items = indentItemMapper.selectList(queryWrapper);
            ArrayList<IndentItemVO> itemVOs = new ArrayList<>();
            for (IndentItem item : items) {
                IndentItemVO itemVO = new IndentItemVO();
                BeanUtils.copyProperties(item,itemVO);
                Good good = goodMapper.selectById(item.getGood());
                BeanUtils.copyProperties(good,itemVO);
                String imagePath = good.getImagePath();
                itemVO.setImagePath( imagePath.split(";")[0]);
                itemVO.setGood(good.getGoodName());
                itemVO.setGoodId(good.getGoodId());
                itemVO.setUnivalence(good.getPrice());
                itemVOs.add(itemVO);
            }
            indentVO.setIndentItems(itemVOs);
            indentVOs.add(indentVO);
        }
        return indentVOs;
    }

    @Override
    public List<IndentVO> getSuccessIndents(Integer customerId) {
        List<IndentVO> indentVOs = new ArrayList<>();
        QueryWrapper queryWrapper1 = new QueryWrapper();
//        queryWrapper1.eq("indent_status",indentStatus);
        queryWrapper1.eq("customer",customerId);
        queryWrapper1.eq("indent_status",2);
        List<Indent> indents = indentMapper.selectList(queryWrapper1);

        for (Indent indent : indents) {
            IndentVO indentVO = new IndentVO();
            BeanUtils.copyProperties(indent,indentVO);
            indentVO.setCustomer(customerMapper.selectById(indent.getCustomer()).getCustomerName());
            indentVO.setAddress(addressMapper.selectById(indent.getAddress()).getAddressName());
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("indent",indent.getIndentId());
            List<IndentItem> items = indentItemMapper.selectList(queryWrapper);
            ArrayList<IndentItemVO> itemVOs = new ArrayList<>();
            for (IndentItem item : items) {
                IndentItemVO itemVO = new IndentItemVO();
                BeanUtils.copyProperties(item,itemVO);
                Good good = goodMapper.selectById(item.getGood());
                BeanUtils.copyProperties(good,itemVO);
                String imagePath = good.getImagePath();
                itemVO.setImagePath( imagePath.split(";")[0]);
                itemVO.setGood(good.getGoodName());
                itemVO.setGoodId(good.getGoodId());
                itemVO.setUnivalence(good.getPrice());
                itemVOs.add(itemVO);
            }
            indentVO.setIndentItems(itemVOs);
            indentVOs.add(indentVO);
        }
        return indentVOs;

    }

    @Override
    public String editPassword(Integer customerId, String oldPass, String newPass) {
        Customer customer = customerMapper.selectById(customerId);
        if (oldPass.equals(customer.getPassword())){
            customer.setPassword(newPass);
            customerMapper.updateById(customer);
            return "修改成功";
        }

        return "旧密码有误，请重新输入密码";
    }

    @Override
    public Customer getCustomer(Integer customerId) {
        Customer customer = customerMapper.selectById(customerId);
        return customer;
    }


}
